import * as path from 'node:path'
import fs from 'node:fs'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// yml文件读取插件
import content from '@originjs/vite-plugin-content'

// 兼容性插件
import legacy from '@vitejs/plugin-legacy'

// 读取yml配置
import yaml from 'js-yaml'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const config = yaml.load(fs.readFileSync('./config/application.yml', 'utf8'))

  return {
    base: '/mono-tpl-vue3-framework-pc/',
    build: {
      // 不分割css减少http
      cssCodeSplit: false,
      reportCompressedSize: false, // 计算打包时间关闭，不进行该环节；提升bundle效率
      sourcemap: false, // 生成sourcemap
      minify: false, // 去格式化
      target: ['es2015'], // 处理语法转译
    },
    plugins: [
      vue(),
      vueJsx(),
      content(),
      legacy({
        targets: ['defaults', 'last 5 version', '> 1%', 'not ie <= 8'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        modernPolyfills: true,
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        // packages和framework的别名
        '@framework': path.resolve(__dirname, './framework'),
      },
      extensions: ['.js', '.vue', '.json', '.ts', '.tsx'],
    },
    server: {
      host: '0.0.0.0',
      port: 3000, // 设置服务启动端口号
      open: false, // 设置服务启动时是否自动打开浏览器
      https: false,
      cors: true, // 允许跨域
      hmr: true,
    },
  }
})
